package org.wu.framework.lazy.orm.core.persistence.reverse.lazy.similar;


import org.wu.framework.lazy.orm.core.config.ReverseEngineering;
import org.wu.framework.lazy.orm.core.persistence.reverse.ParamDescribeList;
import org.wu.framework.lazy.orm.core.persistence.reverse.ReverseClassLazyTableEndpoint;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.DefaultAbstractJavaReverseEngineeringMethod;

/**
 * 接口Mapper 通用映射
 */
public abstract class DefaultAbstractJavaReverseEngineeringMapperMethod extends DefaultAbstractJavaReverseEngineeringMethod {

    protected DefaultAbstractJavaReverseEngineeringMapperMethod(ReverseClassLazyTableEndpoint reverseClassLazyTableEndpoint, ReverseEngineering reverseEngineering) {
        super(reverseClassLazyTableEndpoint, reverseEngineering);
    }


    /**
     * description:  添加导入的class
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initImportClassName() {
        super.initImportClassName();

        addImportClassName("org.apache.ibatis.annotations.Mapper");
        addImportClassName("org.apache.ibatis.annotations.Param");
        addImportClassName("com.baomidou.mybatisplus.extension.plugins.pagination.Page");
    }

    /**
     * description:  添加 class 上的注解
     * class 上的注解如  @LazyTableFieldId(name = "%s", comment = "%s")
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassAnnotationPart() {
        super.initClassAnnotationPart();
        addClassAnnotationPart("@Mapper");
    }


    /**
     * 获取 模型 class 并且将第一个字母小写
     * @return 模型 class 并且将第一个字母小写
     */
    public abstract String getModelClassLowercaseFirstLetter();

    /**
     * 获取 模型class
     * @return 模型class
     */
    public abstract String getModelClassName();

    /**
     * 获取 模型class 完整地址
     * @return 模型class 完整地址
     */
    public abstract String getModelClass();

    /**
     * description:  添加 class 方法片段
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassMethodPart() {
        super.initClassMethodPart();
        if (this.getReverseEngineering().isEnableNormalCrud()) {
            String comment = this.getReverseClassLazyTableEndpoint().getComment();
            String className = getModelClassName();

            String methodParamName = getModelClassLowercaseFirstLetter();
            String saveMethodDescribe = generateMethodDescribe("新增" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "新增" + comment)
                            .returnParam("Long", comment + "影响行数")
            );
            // story
            addClassMethodPart(saveMethodDescribe);
            addClassMethodPart(String.format("    Long story(@Param(\"%s\") %s %s);\n",
                    methodParamName,
                    className, methodParamName
            ));
            // batchStory
            addClassMethodPart(generateMethodDescribe("批量新增" + comment
                    ,
                    ParamDescribeList
                            .of()
                            .param(String.format("%sList", methodParamName), "批量新增" + comment)
                            .returnParam("Long", comment + "影响行数")
            ));
            addClassMethodPart(String.format("    Long batchStory(@Param(\"%sList\") List<%s> %sList);\n",
                    methodParamName,
                    className, methodParamName
            ));
            // update
            String updateMethodDescribe = generateMethodDescribe("更新" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "更新" + comment)
                            .returnParam("Long", comment + "影响行数")
            );
            addClassMethodPart(updateMethodDescribe);
            addClassMethodPart(String.format("    Long update(@Param(\"%s\") %s %s);\n",
                    methodParamName,
                    className, methodParamName
            ));
            // findOne
            String findOneMethodDescribe = generateMethodDescribe("查询单个" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "查询单个" + comment)
                            .returnParam(String.format("%s", className), comment + "模型对象")
            );
            addClassMethodPart(findOneMethodDescribe);
            addClassMethodPart(String.format("    %s findOne(@Param(\"%s\") %s %s);\n",
                    className,
                    methodParamName,
                    className, methodParamName
            ));
            // findList
            String findListMethodDescribe = generateMethodDescribe("查询多个" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "查询多个" + comment)
                            .returnParam(String.format("List<%s>", className), comment + "模型对象")
            );
            addClassMethodPart(findListMethodDescribe);
            addClassMethodPart(String.format("    List<%s> findList(@Param(\"%s\") %s %s);\n",
                    className, methodParamName,
                    className, methodParamName
            ));
            // findPage
            String findPageMethodDescribe = generateMethodDescribe("分页查询多个" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "分页查询多个" + comment)
                            .returnParam(String.format("Page<%s>", className), "分页" + comment + "模型对象")
            );
            addClassMethodPart(findPageMethodDescribe);
            addClassMethodPart(String.format("    Page<%s> findPage(@Param(\"page\") Page page,@Param(\"%s\") %s %s);\n",
                    className, methodParamName,
                    className, methodParamName
            ));
            // remove
            String deleteMethodDescribe = generateMethodDescribe("删除" + comment,
                    ParamDescribeList
                            .of()
                            .param(String.format("%s", methodParamName), "删除" + comment)
                            .returnParam("Long", comment));
            addClassMethodPart(deleteMethodDescribe);
            addClassMethodPart(String.format("    Long remove(@Param(\"%s\") %s %s);\n",
                     methodParamName,
                    className, methodParamName
            ));
        }
    }
}
